package com.uroad.lib.activity;

import android.os.Bundle;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.uroad.lib.R;
import com.uroad.lib.json.FastJsonUtils;
import com.uroad.lib.widget.loadmore.LoadMoreRecyclerView;
import com.uroad.lib.widget.pulltorefresh.DefultPtrHandler;
import com.uroad.lib.widget.pulltorefresh.MaterialPullToRefreshLayout;
import com.uroad.lib.widget.pulltorefresh.PtrHandler;

/**
 * Created by jiadong on 2016/11/11.
 */
public abstract class BaseRefreshRvActivity extends BaseUroadActivity {
    public MaterialPullToRefreshLayout mprl_ptr_framelayout;
    public LoadMoreRecyclerView lv_load_more;
    public PtrHandler ptrHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setBaseContentLayout(R.layout.activity_base_pull_recyclerview);
        mprl_ptr_framelayout = (MaterialPullToRefreshLayout) findViewById(R.id.mprl_ptr_framelayout);
        lv_load_more = (LoadMoreRecyclerView) findViewById(R.id.lv_load_more);
        initViewData();
        mprl_ptr_framelayout.setPtrHandler(new DefultPtrHandler() {
            @Override
            public void onRefresh() {
                pullToLoadData();
            }
        });

        lv_load_more.setLoadMoreListener(new LoadMoreRecyclerView.OnLoadMoreListener() {
                    @Override
                    public void onLoadMore() {
                        loadMoreData();
                    }
                });
    }

    protected abstract void initViewData();

    protected abstract void loadMoreData();

    public abstract void pullToLoadData();

    @Override
    public void OnHttpTaskComplete(String result, String type) {
        JSONObject object = JSON.parseObject(result);
        if (FastJsonUtils.getJsonStatuByInt(object)) {
            handleJsonData(object, type);
        } else {
            jsonStatuFail(object, type);
        }
        mprl_ptr_framelayout.refreshComplete();
    }

    /**
     * success to load data from net
     *
     * @param object
     * @param type
     */
    protected abstract void handleJsonData(JSONObject object, String type);

    /**
     * fail to load data from net
     *
     * @param object
     * @param type
     */
    protected void jsonStatuFail(JSONObject object, String type){
        lv_load_more.setCanLoadMore(false);
    }

}
